<template>
    <div class="app-container">

        <!-- 添加修改弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="800px"
                center
                append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="标题" prop="noticeTitle">
                            <el-input v-model="form.noticeTitle" placeholder="请输入通知公告标题" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio
                                        v-for="dict in statusOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictValue"
                                        :value="dict.dictValue"
                                >{{ dict.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="类型" prop="noticeType">
                            <el-radio-group v-model="form.noticeType">
                                <el-radio
                                        v-for="dict in noticeTypeOptions"
                                        :key="dict.dictValue"
                                        :label="dict.dictValue"
                                        :value="dict.dictValue"
                                >{{ dict.dictLabel }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="公告内容">
                            <mavon-editor
                                    ref="noticeContent"
                                    v-model="form.noticeContent"
                                    height="300px"
                                    :options="{hideModeSwitch:true,previewStyle:'tab'}"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" clearable size="small" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
        </el-dialog>
        <!-- 添加修改弹出层结束 -->

        <!--公告内容 弹出层开始 -->
        <el-dialog
                :title="title"
                :visible.sync="noticeContentOpen"
                width="800px"
                center
                append-to-body
        >
            <mavon-editor ref="noticeContent" v-model="noticeContent" :aria-disabled="true" :options="{hideModeSwitch:true,previewStyle:'tab'}" />
        </el-dialog>
        <!-- 公告内容弹出层结束 -->

        <!-- 查询条件开始 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="公告标题" prop="noticeTitle">
                <el-input
                        v-model="queryParams.noticeTitle"
                        placeholder="请输入通知公告标题"
                        clearable
                        size="small"
                        style="width:140px"
                />
            </el-form-item>
            <el-form-item label="发布者" prop="createBy">
                <el-input
                        v-model="queryParams.createBy"
                        placeholder="请输入发布者"
                        clearable
                        size="small"
                        style="width:140px"
                />
            </el-form-item>
            <el-form-item label="类型" prop="noticeType">
                <el-select
                        v-model="queryParams.noticeType"
                        placeholder="类型"
                        clearable
                        size="small"
                        style="width:140px"
                >
                    <el-option
                            v-for="dict in noticeTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                        v-model="queryParams.status"
                        placeholder="可用状态"
                        clearable
                        size="small"
                        style="width:210px"
                >
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" circle @click="queryTable"></el-button>
                <el-button type="warning" icon="el-icon-refresh" circle @click="refreshDictType"></el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->

        <!-- 表格工具按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="addNotice">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
            </el-col>
        </el-row>
        <!-- 表格工具按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table  border :data="noticeTableList" :ref="multipleTable" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="通知ID" align="center" prop="noticeId" sortable/>
            <el-table-column label="标题" align="center" prop="noticeTitle" />
            <el-table-column label="通知类型" prop="noticeType" align="center" :formatter="noticeFormat" />
            <el-table-column label="状态" prop="status" align="center" :formatter="statusFormat" />
            <el-table-column label="发布者" align="center" prop="createBy" />
            <el-table-column label="创建时间" align="center" prop="createTime" sortable/>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" size="mini" @click="updNotice(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    <el-button type="text" icon="el-icon-view" size="mini" @click="handleView(scope.row)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->

        <!-- 分页控件开始 -->
        <el-pagination
                v-show="page.total>0"
                :current-page="page.current"
                :page-sizes="[5, 10, 20, 30]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        />
        <!-- 分页控件结束 -->
    </div>
</template>

<script>
    export default {
        data() {
            return {
                //添加修改表单标题
                title:'',
                //添加/修改表单的控值
                dialogVisible:false,
                //添加修改表单对象
                form:{
                    noticeType: '0',
                    status: '0',
                },
                //存放被选中的数据id
                ids:[],
                //模糊查询的条件
                queryParams:{},
                //表格数据
                noticeTableList:[],
                //表格中被选中的数据
                multipleTable:[],
                // 非多个禁用
                multiple: true,
                //初始化状态数据
                statusOptions:[],
                //初始化通知类型数据
                noticeTypeOptions:[],
                //通知公告显示、隐藏的控值
                noticeContentOpen:false,
                // 查看内容
                noticeContent: undefined,
                // 表单校验
                rules: {
                    noticeTitle: [
                        { required: true, message: '通知公告标题不能为空', trigger: 'blur' }
                    ]
                },
                //分页数据
                page:{
                    current:1,
                    size:5,
                    total:0
                },
            }
        },
        created() {
            this.initTable();
            this.initStatus();
            this.initNotice();
        },
        methods: {
            /* 表格外点击按钮触发事件 */
            queryTable(){    //模糊查询按钮触发
                this.page.current=1;
                this.initTable();
            },
            refreshDictType(){  //重置模糊查询按钮触发
                this.queryParams={};
                this.initTable();
            },
            addNotice(){  //添加按钮触发添加通知公告
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                //弹出添加/修改表单
                this.dialogVisible=true;
                this.title = '添加通知公告';
                this.form={
                    noticeType: '0',
                    status: '0',
                };
            },
            /*表格内点击事件*/
            handleSelectionChange(selection) { // 数据表格的多选择框选择时触发
                this.ids = selection.map(item => item.noticeId)
                //没有行被选中的时候，将批量删除按钮禁用
                this.multiple = !selection.length
            },
            handleDelete(row){  //删除按钮触发事件
                //哪一个有值就将哪一个赋值给dictIds
                const noticeIds = row.noticeId || this.ids
                this.$confirm('此操作将永久删除被选中的通知公告, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete(`/system/api/notice/deleteNoticeByIds/${noticeIds}`).then(result=>{
                        if (result.data.data){
                            this.$message({
                                showClose: true,
                                message: result.data.msg,
                                type: 'success'
                            });
                        }else {
                            this.$message({
                                showClose: true,
                                message: '操作失败',
                                type: 'error'
                            });
                        }
                        this.page.current=1;
                        this.initTable();
                    })
                }).catch(() => {
                    this.$message.info('删除已取消')
                })
            },

            /*表格内点击事件*/
            updNotice(row){  //修改通知公告
                if (this.$refs.form != undefined) {
                    this.$refs.form.clearValidate();
                }
                this.title='修改通知公告';
                //显示弹出对话框
                this.dialogVisible = true;
                //将当前行的字典数据ID赋值给表单
                this.form.noticeId = row.noticeId;
                //采取深克隆的形式
                this.form = JSON.parse(JSON.stringify(row));
            },
            // 打开修改的弹出层
            handleView(row) {    //查看公告内容
                this.title = row.noticeTitle;
                this.noticeContentOpen = true;
                this.noticeContent = row.noticeContent;
            },

            /*表单内点击事件*/
            handleSubmit(){  // 添加/修改表单确定按钮触发
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.$http.post("/system/api/notice/saveOrUpdateNotice",this.form).then(result=>{
                            if (result.data.data){
                                this.$message({
                                    showClose: true,
                                    message: result.data.msg,
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    showClose: true,
                                    message: '操作失败',
                                    type: 'error'
                                });
                            }
                            // 关闭弹出对话框
                            this.dialogVisible = false;
                            // 重置表单
                            // this.reset();
                            this.title='';
                            //表格回溯到第一页
                            this.page.current=1;
                            // 刷新表格数据
                            this.initTable();
                        })
                    }
                })
            },
            cancel(){     //添加修改表单取消按钮触发
                this.dialogVisible=false;
                this.title = '';
            },

            /*  初始化数据  */
            initTable(){  //初始化表格数据
                this.$http.post("/system/api/notice/selectAll/"+this.page.current+"/"+this.page.size,this.queryParams).then(result=>{
                    this.noticeTableList = result.data.data.records;
                    this.page.total = result.data.data.total;
                })
            },
            initStatus(){   //初始化状态字典
                this.$http.get("/system/api/dict/data/findByType/sys_normal_disable").then(result=>{
                    this.statusOptions = result.data.data;
                })
            },
            statusFormat(row){  //状态字典解析
                let v = row.status;
                return this.formatDict2(this.statusOptions,v);
            },
            initNotice(){   //初始化通知公告字典
                this.$http.get("/system/api/dict/data/findByType/sys_notice_type").then(result=>{
                    this.noticeTypeOptions = result.data.data;
                })
            },
            noticeFormat(row){  //通知公告字典解析
                let v = row.noticeType;
                return this.formatDict2(this.noticeTypeOptions,v);
            },
            // 分页pageSize变化时触发
            handleSizeChange(val) {
                this.page.size = val
                // 重新查询
                this.initTable()
            },
            // 点击上一页  下一页，跳转到哪一页面时触发
            handleCurrentChange(val) {
                this.page.current = val
                // 重新查询
                this.initTable()
            },
        }
    }
</script>

<style lang="scss" scoped>
</style>
